<template>
  <div class="replay">
    <van-cell>
      <!-- 自定义插槽复用 -->
      <template #title>
        <div class="cmt-wrap">
          <van-image
            round
            fit="cover"
            :src="currentItem.aut_photo"
          ></van-image>
          <div class="info">
            <div class="name">{{ currentItem.aut_name }}</div>
            <div class="content">{{ currentItem.content }}</div>
            <div class="time">{{ currentItem.pubdate | relvTime
                }}
            </div>
          </div>
        </div>
      </template>
    </van-cell>
    <!-- 分割线 -->
    <van-divider>回复列表</van-divider>

    <!-- 对上面的评论 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.com_id"
      >
        <!-- 自定义插槽复用 -->
        <template #title>
          <div class="cmt-wrap">
            <van-image
              round
              fit="cover"
              :src="item.aut_photo"
            ></van-image>
            <div class="info">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">{{ item.pubdate | relvTime }}
              </div>
            </div>
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>
    <!-- 输入的区域 -->
    <van-field
      v-model="cmtValue"
      center
      clearable
      @keyup.enter="send"
      placeholder="请输入评论内容"
    >
      <template #button>
        <van-button
          size="small"
          type="info"
          @click="send"
        >发布</van-button>
      </template>
    </van-field>
  </div>
</template>

<script>
import { cmtListAPI, sendCmtAPI } from "@/api";
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,

      // 输入的评论内容
      cmtValue: "",
      offset: undefined,
    };
  },
  props: {
    currentItem: {
      type: Object,
      require: true
    }
  },
  methods: {
    // 渲染评论列表信息
    async onLoad() {
      const res = await cmtListAPI({
        type: "c", // c-对评论(comment)的回复
        source: this.currentItem.com_id, //传过来对象项的id
        offset: this.offset,
      });
      this.list.push(...res.data.data.results)
      // 把本次的最后一条id作为下次请求的参数
      this.offset = res.data.data.last_id
      this.loading = false

      //  当本次的最后一条id，等于 所有数据的结束id就意味着加载完了
      if (res.data.data.end_id == res.data.data.last_id) {
        this.finished = true;
      }

    },
    // 点击发送 
    async send() {
      //发表评论
      if (this.cmtValue === '') {
        this.$toast('请输入内容')
        return
      }
      const res2 = await sendCmtAPI({
        content: this.cmtValue, // 回复的内容
        target: this.currentItem.com_id, // 当前评论的id
        // 接口规定发表回复必须传文章id
        art_id: this.$route.query.id,
      })
      this.list.unshift(res2.data.data.new_obj)
      this.cmtValue = ''
      this.$toast('回复成功')
    }
  }
}
</script>

<style>
</style>